// C11:Interrupting2.cpp
//  Interrupting a thread blocked
//  With a synchronization guard.
//  {L} zThread
#include "zthread/Guard.h"
#include "zthread/Mutex.h"
#include "zthread/Thread.h"
#include <iostream>

using namespace zThread;
using namespace std;

class BlockedMutex {
  Mutex lock;

public:
  BlockedMutex() { lock.acquire(); }

  void f() {
    Guard<Mutex> g(lock);
    // This will never be available
  }
};

class Blocked2 : public Runnable {
  BlockedMutex blocked;

public:
  void run() {
    try {
      cout << "Waiting for f() int BlockedMutex" << endl;
      blocked.f();
    } catch (Interrupted_Exception &e) {
      cerr << e.what() << endl;
      // Exit the task
    }
  }
};

int main(int argc, char *argv[]) {
  try {
    Thread t(new Blocked2);
    t.interrupt();
  } catch (Synchronization_Exception &e) {
    cerr << e.what() << endl;
  }
}